<template>
  <el-row>
    <el-col :xl="12" :lg="15" class="cert-item">
      <div class="edit">
        <span @click="handleClose">取消</span>
        <span @click="handleUpdatePassword">保存</span>
      </div>
      <el-row class="marginT10">
        <el-col :span="20">
          <el-form
            ref="updatePasswordForm"
            label-width="86px"
            :model="passwordForm"
            :rules="passwordRules"
          >
            <el-form-item label="原密码" prop="oldPassword">
              <el-input v-model="passwordForm.oldPassword" size="medium" show-password />
            </el-form-item>
            <el-form-item label="新密码" prop="newPassword">
              <el-input v-model="passwordForm.newPassword" size="medium" show-password />
            </el-form-item>
            <el-form-item label="确认密码" prop="certainPassword">
              <el-input v-model="passwordForm.certainPassword" size="medium" show-password />
            </el-form-item>
          </el-form>
        </el-col>
      </el-row>
    </el-col>
  </el-row>
</template>

<script>
// 接口功能引入
import { checkOldPassword, updatePassword } from '@/api/system/profile'
export default {
  name: 'UpdatePassword',
  data() {
    const passwordLength = (rule, value, callback) => {
      if (value.length < 6 || value.length > 20) {
        return callback(new Error('密码不能少于6位且不能大于20位'))
      } else {
        callback()
      }
    }
    const newPassword = (rule, value, callback) => {
      if (value.length < 6 || value.length > 20) {
        return callback(new Error('密码不能少于6位且不能大于20位'))
      } else if (value === this.passwordForm.oldPassword) {
        return callback(new Error('新密码不能与旧密码相同'))
      } else {
        callback()
      }
    }
    const validateCertainPassword = (rule, value, callback) => {
      if (!value) {
        return callback(new Error('确认密码不得为空'))
      } else {
        const newPassword = this.passwordForm.newPassword
        if (value !== newPassword) {
          return callback(new Error('两次输入的密码不一致！请重新输入'))
        } else {
          callback()
        }
      }
    }
    return {
      // 修改密码表单绑定数据
      passwordForm: {
        oldPassword: '',
        newPassword: '',
        certainPassword: ''
      },
      // 修改密码表单绑定数据
      passwordRules: {
        oldPassword: [
          { required: true, message: '原密码不得为空', trigger: 'blur' },
          { required: true, validator: passwordLength, trigger: 'blur' }
        ],
        newPassword: [
          { required: true, message: '新密码不得为空', trigger: 'blur' },
          { required: true, validator: newPassword, trigger: 'blur' }
        ],
        certainPassword: [
          {
            required: true,
            validator: validateCertainPassword,
            trigger: 'blur'
          }
        ]
      }
    }
  },
  methods: {
    // 关闭按钮操作
    handleClose() {
      this.$store.dispatch('tagsView/delView', this.$route)
      this.$router.push({ path: '/index' })
    },
    // 更改密码修改操作
    handleUpdatePassword() {
      this.$refs['updatePasswordForm'].validate(async(val) => {
        if (val) {
          const { data } = await checkOldPassword(this.passwordForm.oldPassword)
          if (data === '') {
            this.warningMsg('您输入的密码与原密码不一致,请重新输入')
            return
          }
          const oldPassword = this.passwordForm.oldPassword
          const newPassword = this.passwordForm.newPassword
          await updatePassword({ oldPassword, newPassword })
          this.msgSuccess('密码重置成功')
          this.$store.dispatch('user/resetToken')
          location.href = '/login'
        }
      })
    }
  }
}
</script>

<style lang='scss' scoped>
  @import '@/styles/variables.scss';
.cert-item {
  border: 1px solid #e0d7d7;
  border-radius: 4px;
  padding: 15px 10px 10px;
  overflow: hidden;
  margin-bottom: 15px;
  /*margin-left: 30px;*/
  position: relative;
  .edit {
    width: 100%;
    height: 30px;
    border-bottom: 1px solid #ededed;
    text-align: right;
    font-size: 14px;
    color: $blue;
    span {
    cursor: pointer;
      margin-left: 10px;
    }
  }
}
</style>
